package com.ming.luckyDemo.controller;

import com.ming.luckyDemo.pojo.DO.UserInfoDO;
import com.ming.luckyDemo.pojo.entity.ResultBody;
import com.ming.luckyDemo.service.RegisterService;
import com.ming.luckyDemo.util.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Objects;
import java.util.Random;
import java.util.UUID;


/**
 * @author Tcm
 */
@RestController
public class RegisterController {

    @Autowired
    RegisterService registerService;

    @PostMapping("/register")
    public ResultBody register(UserInfoDO userInfoDO) {
        if ("".equals(userInfoDO.getUsername()) ||
                "".equals(userInfoDO.getPassword()) ||
                "".equals(userInfoDO.getEmail())){
            return ResultBody.error().message("用户信息不能为空！");
        }else {
            UserInfoDO repeat = registerService.queryByName(userInfoDO.getUsername());
            if (repeat!=null){
                return ResultBody.error().message("用户名被占用");
            }else {
                registerService.insertUser(userInfoDO);
                return ResultBody.ok().message("注册成功");
            }
        }
    }

    @PostMapping("/login")
    public ResultBody login(HttpServletRequest request,HttpServletResponse response,UserInfoDO userInfoDO) {
        if ("".equals(userInfoDO.getUsername()) ||
                "".equals(userInfoDO.getPassword())) {
            return ResultBody.error().message("用户信息不能为空！");
        }else {
            Cookie[] cookies = request.getCookies();
            if (Objects.isNull(cookies)) {
                //生成用户唯一标识uuid
                Random random = new Random();
                int end = random.nextInt(999);
                //如果不足三位前面补0
                String endStr = String.format("%03d", end);
                String sessionKey = UUID.randomUUID().toString().replace("-", "") + endStr;

                //存入DO
                userInfoDO.setSessionKey(sessionKey);
                //查看是否存在该用户
                UserInfoDO repeat = registerService.queryByName(userInfoDO.getUsername());
                if (repeat != null) {
                    boolean login = registerService.login(userInfoDO);
                    if (login){
                        System.out.println("登录成功");
                        //将用户名和标识符添加到cookie发送到客户端（浏览器）
                        Cookie username = new Cookie(userInfoDO.getUsername(),sessionKey);
                        response.addCookie(username);
                        response.setHeader("Set-Cookie", response.getHeader("Set-Cookie")+";SameSite=None");

                        return ResultBody.ok().message("登录成功");
                    }
                    System.out.println("密码错了");
                    return ResultBody.error().message("密码错误");
                } else {
                    return ResultBody.error().message("该用户名不存在");
                }
            }
            for (Cookie cookie : cookies) {
                UserInfoDO userInfoDO1 = registerService.login2(cookie.getName());

                if (cookie.getName().equals(userInfoDO1.getUsername()) && cookie.getValue().equals(userInfoDO1.getSessionKey())) {
                    return ResultBody.ok().code(200).message("登录成功");
                }
                return null;
            }
            return ResultBody.error().message("重新登录");
        }
    }
//    @ModelAttribute
//    public void setReqAndRes(HttpServletRequest request,HttpServletResponse response) {
//        response.setHeader("Set-Cookie", response.getHeader("Set-Cookie")+";SameSite=None");
//
//    }
}
